﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace XNAGameFeatures.Input.Device
{
    /// <summary>
    /// 
    /// </summary>
    public class KeybordInput : InputDevice
    {

        #region BaseInput

        private readonly KeyboardState[] _current = new KeyboardState[MaxInput];
        private readonly KeyboardState[] _old = new KeyboardState[MaxInput];

        internal KeybordInput()
        {

        }

        /// <summary>
        /// Updates all input entry.
        /// </summary>
        internal override void Update()
        {
            for (int i = 0; i < _current.Length; i++)
            {
                _old[i] = _current[i];

                _current[i] = Keyboard.GetState((PlayerIndex)i);
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Determines whether if the key is press once for the specific player.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="player">The player.</param>
        /// <returns>
        ///   <c>true</c> if the key is press once; otherwise, <c>false</c>.
        /// </returns>
        public bool IsKeyPressOnce(Keys key, PlayerIndex player)
        {
            return _current[(int)player].IsKeyDown(key) && _old[(int)player].IsKeyUp(key);
        }

        /// <summary>
        /// Determines whether if the key is press once.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns>
        ///   <c>true</c> if the key is press once; otherwise, <c>false</c>.
        /// </returns>
        public bool IsKeyPressOnce(Keys key)
        {
            return IsKeyPressOnce(key, PlayerIndex.One)
                || IsKeyPressOnce(key, PlayerIndex.Two)
                || IsKeyPressOnce(key, PlayerIndex.Three)
                || IsKeyPressOnce(key, PlayerIndex.Four);
        }

        /// <summary>
        /// Determines whether if the key is press for the specific player.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="player">The player.</param>
        /// <returns>
        ///   <c>true</c> if the key is press; otherwise, <c>false</c>.
        /// </returns>
        public bool IsKeyPress(Keys key, PlayerIndex player)
        {
            return _current[(int)player].IsKeyDown(key);
        }

        /// <summary>
        /// Determines whether if the key is press.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns>
        ///   <c>true</c> if the key is press; otherwise, <c>false</c>.
        /// </returns>
        public bool IsKeyPress(Keys key)
        {
            return IsKeyPress(key, PlayerIndex.One)
                || IsKeyPress(key, PlayerIndex.Two)
                || IsKeyPress(key, PlayerIndex.Three)
                || IsKeyPress(key, PlayerIndex.Four);
        }

        #endregion

    }
}
